#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include "json/json.h"
#include "Log.hpp"

class FileUtil {
public:
    FileUtil():filename_("Server.conf")
    {}
    long FileSize() {
        struct stat st;
        if(stat(filename_.c_str(), &st) < 0) {
            Logger::getInstance().loggger()->error("获取文件大小失败");
            return -1;
        }
        return st.st_size;
    }
    time_t LastModTime() {
        struct stat st;
        if(stat(filename_.c_str(), &st) < 0) {
            Logger::getInstance().loggger()->error("获取文件最新修改时间失败");
            return -1;
        }
        return st.st_mtime;
    }
    bool GetContent(std::string *body) {
        long FSize = FileSize(); //获取配置文件大小
        std::ifstream ifs;
        ifs.open(filename_, std::ios::binary);
        if(ifs.is_open() == false) {
            Logger::getInstance().loggger()->error("GetPosLen, 打开文件失败");
            return false;
        }
        body->resize(FSize);
        ifs.read(&(*body)[0], FSize);
        if(ifs.good() == false) {
            Logger::getInstance().loggger()->error("GetPosLen, 读取文件失败");
            ifs.close();
            return false;
        }
        ifs.close();
        return true;
    }
private:
    const std::string filename_;
};

class JsonUtil {
public:
    static bool UnSerialize(const std::string &str, Json::Value *root) {
        Json::Reader reader;
        if (!reader.parse(str, *root)) {
            Logger::getInstance().loggger()->fatal("读取配置文件失败");
            return false;
        }
        return true;
    }
};

